/*
Copyright 2009 Jeff Carnes

This file is part of Fluant.

Fluant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

Fluant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package com.amiasleep.fluant.flaPublisher;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;

import com.amiasleep.fluant.flaPublisher.io.SWFOutput;
import com.amiasleep.fluant.jsfl.CompatibilityException;
import com.amiasleep.fluant.jsfl.JSFLExecuteException;
import com.amiasleep.fluant.jsfl.JSFLRunner;



public class FLAPublisher extends JSFLRunner {
	
	protected final static int DEFAULT_TIMEOUT = 60000;
	public Profile profile;
	public boolean closeFLAWhenFinished = false;
	
	
	public FLAPublisher() throws CompatibilityException
	{
		super();
	}
	
	
	
	public FLAPublisher(int timeout) throws CompatibilityException
	{
		super();
		this.timeout = timeout;
	}
	
	
	
	public synchronized void run(File fla, Profile profile) throws JSFLExecuteException
	{
		try
		{
			// Write out a profile that we can import into Flash for our publish settings
			File tempProfile = File.createTempFile("fluantProfile", ".xml");
			Writer out = new FileWriter(tempProfile);
			ProfileIO.write(profile, out);
			
			// Create the JSFL that will publish the FLA
			jsflFile = File.createTempFile("fluantPublish", ".jsfl");
			Writer jsflOut = new FileWriter(jsflFile);
			PublishJSFLIO.write(fla, tempProfile, jsflOut, closeFLAWhenFinished);
			
			// Now publish that bad boy
			run();
			
			
			printSWFInfo(profile.getSwf(), fla);
			System.out.println("Successfully published " + fla);
		}
		catch(IOException ioEx)
		{
			throw new JSFLExecuteException(ioEx);
		}
		catch(TemplateOutputException tex)
		{
			throw new JSFLExecuteException(tex);
		}
	}
	
	
	
	private void printSWFInfo(SWFOutput swf, File fla)
	{
		// Check preconditions
		if(swf == null)  return;
		
		
		String swfPath = swf.getFile().getPath();
		URI flaUri, swfUri;
		try
		{
			// Either of these could (but shouldn't) throw a URISyntaxException exception
			flaUri = new URI(fla.getPath());
			swfUri = flaUri.resolve(swfPath);
			
			File swfFile = new File( swfUri.normalize().toString() );
			System.out.println("Published SWF: " + swfUri + " (" + swfFile.length() + " bytes)");
		}
		catch(URISyntaxException use)
		{
			System.out.println("FLAPublisher: Error determining absolute path for SWF based on [" + swfPath + "]");
			System.out.println("FLAPublisher: " + use.toString());
		}
	}
	
}
